
#region Imported Namespaces
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

using PHSRAG.Insight21.SearchContext;
using Policy = PHSRAG.Insight21.Policy;
#endregion

namespace PHSRAG.Insight21.InsightWebControls
{
	/// <summary>
	/// The SearchCriteriaDisplay control is used to display the contents of the current search criteria in a tabular
	/// fashion. The display is in two columns; the first displays the selected key, and the second the value associated
	/// with that key.
	/// Note that for display purposes, the label entries in the search criteria is used instead of the "full" criteria.
	/// This is done to limit the display to what the user would want to see.
	/// This control has no design-time behavior since the search criteria does not exist at that time.
	/// </summary>
	[ToolboxData("<{0}:SearchCriteriaDisplay runat=server />")]
	public class SearchCriteriaDisplay : System.Web.UI.WebControls.WebControl, INamingContainer
	{
		#region Private members
		private string daysProjectEndingLabel = "Days Project Ending";
		#endregion

		#region Constants
		private const string DisplayCommand		= "DisplayCriteria";
		#endregion

		#region Events
		/// <summary>Delegate used by page to decide whether or not to display the criteria. </summary>
		public delegate bool CriteriaDisplayEventHandler(string criteriaKey);

		/// <summary>
		/// Add/Remove handler for the criteria display.
		/// </summary>
		[Category("Action"), Description("Raised when criteria is about to be displayed on the page")]
		public event CriteriaDisplayEventHandler DisplayCriteria 
		{
			add		{ Events.AddHandler(DisplayCommand, value); }
			remove	{ Events.RemoveHandler(DisplayCommand, value); }
		}
		#endregion

		#region Public Properties
		/// <summary>The Projects Ending In xxx Days dropdown control is used in the Agreements Module
		/// on the Salaries Overview page but must be called Salaries on Funds Expiring in xxx Days on
		/// that page.</summary>
		public string DaysProjectEndingLabel
		{
			get{return daysProjectEndingLabel;}
			set{daysProjectEndingLabel = value;}
		}
		#endregion

		#region Overrides
		/// <summary>
		/// Ensure child controls are created and delegate to base class.
		/// </summary>
		public override ControlCollection Controls
		{
			get { EnsureChildControls(); return base.Controls; }
		}

		/// <summary>
		/// Create the display table based on the search criteria and add it to the controls collection.
		/// The display value is actually stored in the criteria as a "label|value" pair which will be split up for display
		/// in two separate columns.
		/// </summary>
		/// <param name="e">Argument associated with the event</param>
		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);

			Policy.ICachePolicy cachePolicy = (Policy.ICachePolicy)Page.Session[Policy.Names.CachePolicyKey];
			SearchCriteria searchCriteria =
				((CommonSearchContext)cachePolicy.GetCachedObject(CommonSearchContext.CacheKey)).GetSearchCriteria(Page.Request.Url.ToString());

			Table table = new Table();
			Controls.Add(table);
			table.CssClass = "SearchCriteriaDisplay";

			foreach (string key in searchCriteria.ControlLabels.Keys)
			{
				string value = (string)searchCriteria.ControlLabels[key];
				if (value != null)
				{
					string[] info = value.Split(CommonSearch.LabelValueSeparator);
					if (info.Length == 2)
					{
						TableRow row = new TableRow();
						table.Rows.Add(row);

						TableCell cell = new TableCell();
						row.Cells.Add(cell);
						cell.CssClass = "CriteriaKey";
						cell.Controls.Add(new LiteralControl(string.Format("Selected {0}:", 
							(key.ToLower() == InsightUtilities.Constants.DaysProjectEndingKey.ToLower()) 
							? DaysProjectEndingLabel : info[0].Replace(":", "").Trim())));

						cell = new TableCell();
						row.Cells.Add(cell);
						cell.CssClass = "CriteriaValue";
						cell.Controls.Add(new LiteralControl(info[1].Trim()));

						row.Visible = DisplaySearchCriteria(key);
					}
				}			
			}
		}
		#endregion

		#region Private Methods
		/// <summary>
		/// Decides whether to display the criteria or not.
		/// </summary>
		/// <param name="criteriaKey">The criteria key</param>
		/// <returns>Boolean indicating whether or not to display the criteria.</returns>
		public bool DisplaySearchCriteria (string criteriaKey) 
		{
			CriteriaDisplayEventHandler criteriaDisplayEvent = Events[DisplayCommand] as CriteriaDisplayEventHandler;
			return criteriaDisplayEvent != null ? criteriaDisplayEvent(criteriaKey) : true;
		}
		#endregion
	}
}

// --- EOF ---
